<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

Class MY_Controller extends CI_Controller
{
    private $intance;
	/** Model */
	public $model;
	
	/** Dados enviado a view */
    private $_data = array();
	
	/**
	 * Construtor da classe.
	 * Criar contrutor em todos os controllers que forem criados.
	 * */
	function __construct() {
		parent::__construct();
		$this->intance = get_instance();
		$this->set_data('base_url', base_url());
		$this->set_data('assets_url', base_url()."assets/");

		//Idioma
		$lang = "";
		//Caso seja o url padrão que não tenha seggmento não faz a validação do addendum
		if(count($this->intance->uri->segments) > 0) {
			$lang = cur_lang($this->intance->uri->segments[1]);
			/**
			 * Validação do annotations
			 * */
			$validation = new Validations_addendum($this->session, $this->intance, $this->intance->router->method);
			$validation->authorize();
		}
		
		$this->load->model(strtolower(get_class($this->intance))."_model", 'model');
	}
	
	/**
	 * Carrega o template usando o twiggi
	 * @param $view String Pagina a ser carregada.
	 */
	public function load_view($view = NULL) {
		$this->twiggy->set($this->_data);
		
		if($view)
			$this->twiggy->template($view)->display();
		else
			$this->twiggy->display();
	}
	
	/**
	 * Checks if it's post.
	 * @return boolean TRUE if it's a post, FALSE otherwise.
	 * */
	public function is_post() {
		return ($_SERVER['REQUEST_METHOD'] == 'POST');
	}
	
	/**
	 * Add information to view.
	 * @param $key String Name to variable
	 * @param $value Mix Value to variable
	 * */
    protected final function set_data($key, $value) {
        $this->_data[$key] = $value;
    }
	
	/**
	 * Checks if it's ajax
	 * @return boolean TRUE if it's ajax
	 * */
	public function isAjax(){
		return (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest');
	}
}